package com.freesun.shop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freesun.shop.domin.ProdSolr;
import com.freesun.shop.entity.HotSearch;
import com.freesun.shop.service.HotSearchService;
import com.freesun.shop.service.SearchService;
import com.freesun.shop.vo.HotSearchVo;
import io.swagger.annotations.ApiOperation;
import ma.glasnost.orika.MapperFacade;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SearchController {

    @Reference(check = false)
    private SearchService searchService;

    @Reference(check = false)
    private HotSearchService hotSearchService;

    @Autowired
    private MapperFacade mapperFacade;

    @ApiOperation("获取商品的条件查询")
    @GetMapping("/search/searchProdPage")
    public ResponseEntity<IPage<ProdSolr>> searchProdSolr(Page<ProdSolr> page
            , String prodName, Integer sort) {
        IPage<ProdSolr> iPage = searchService.search(page, prodName, sort);
        return ResponseEntity.ok(iPage);
    }

    @ApiOperation("根据店铺的id 获取热搜")
    @GetMapping("/search/hotSearchByShopId")
    public ResponseEntity<List<HotSearchVo>> getHotSearchByShopId(Integer number, Integer shopId, Integer sort) {

        List<HotSearch> hotSearches= hotSearchService.topN(number, shopId, sort);
        List<HotSearchVo> hotSearchVos = mapperFacade.mapAsList(hotSearches, HotSearchVo.class);
        return ResponseEntity.ok(hotSearchVos);
    }

}
